package com.daxue.lcm.api.grpc.config;

import java.time.Duration;

/**
 * @author daxue0929
 * @date 2022/9/28
 */

public class GrpcServiceInfo {


    private boolean enabled = true;
    private String target;
    private String host;
    private int port;
    private GrpcServiceInfo.ChannelConfig channel = new GrpcServiceInfo.ChannelConfig();
    private GrpcServiceInfo.StubConfig stub = new GrpcServiceInfo.StubConfig();

    public GrpcServiceInfo() {
    }

    public boolean isEnabled() {
        return this.enabled;
    }

    public String getTarget() {
        return this.target;
    }

    public String getHost() {
        return this.host;
    }

    public int getPort() {
        return this.port;
    }

    public GrpcServiceInfo.ChannelConfig getChannel() {
        return this.channel;
    }

    public GrpcServiceInfo.StubConfig getStub() {
        return this.stub;
    }

    public void setEnabled(final boolean enabled) {
        this.enabled = enabled;
    }

    public void setTarget(final String target) {
        this.target = target;
    }

    public void setHost(final String host) {
        this.host = host;
    }

    public void setPort(final int port) {
        this.port = port;
    }

    public void setChannel(final GrpcServiceInfo.ChannelConfig channel) {
        this.channel = channel;
    }

    public void setStub(final GrpcServiceInfo.StubConfig stub) {
        this.stub = stub;
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof GrpcServiceInfo)) {
            return false;
        } else {
            GrpcServiceInfo other = (GrpcServiceInfo)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.isEnabled() != other.isEnabled()) {
                return false;
            } else {
                label65: {
                    Object this$target = this.getTarget();
                    Object other$target = other.getTarget();
                    if (this$target == null) {
                        if (other$target == null) {
                            break label65;
                        }
                    } else if (this$target.equals(other$target)) {
                        break label65;
                    }

                    return false;
                }

                Object this$host = this.getHost();
                Object other$host = other.getHost();
                if (this$host == null) {
                    if (other$host != null) {
                        return false;
                    }
                } else if (!this$host.equals(other$host)) {
                    return false;
                }

                if (this.getPort() != other.getPort()) {
                    return false;
                } else {
                    Object this$channel = this.getChannel();
                    Object other$channel = other.getChannel();
                    if (this$channel == null) {
                        if (other$channel != null) {
                            return false;
                        }
                    } else if (!this$channel.equals(other$channel)) {
                        return false;
                    }

                    Object this$stub = this.getStub();
                    Object other$stub = other.getStub();
                    if (this$stub == null) {
                        if (other$stub != null) {
                            return false;
                        }
                    } else if (!this$stub.equals(other$stub)) {
                        return false;
                    }

                    return true;
                }
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof GrpcServiceInfo;
    }

    public int hashCode() {
        boolean PRIME = true;
        int result = 1;
        result = result * 59 + (this.isEnabled() ? 79 : 97);
        Object $target = this.getTarget();
        result = result * 59 + ($target == null ? 43 : $target.hashCode());
        Object $host = this.getHost();
        result = result * 59 + ($host == null ? 43 : $host.hashCode());
        result = result * 59 + this.getPort();
        Object $channel = this.getChannel();
        result = result * 59 + ($channel == null ? 43 : $channel.hashCode());
        Object $stub = this.getStub();
        result = result * 59 + ($stub == null ? 43 : $stub.hashCode());
        return result;
    }

    public String toString() {
        return "GrpcServiceInfo(enabled=" + this.isEnabled() + ", target=" + this.getTarget() + ", host=" + this.getHost() + ", port=" + this.getPort() + ", channel=" + this.getChannel() + ", stub=" + this.getStub() + ")";
    }

    public static class StubConfig {
        static final Duration MAX_DEADLINE = Duration.ofSeconds(5L);
        static final Duration DEFAULT_DEADLINE = Duration.ofSeconds(2L);
        static final Duration MIN_DEADLINE = Duration.ofMillis(500L);
        private Duration deadline;

        public Duration getDeadline() {
            if (this.deadline.compareTo(MIN_DEADLINE) < 0) {
                return MIN_DEADLINE;
            } else {
                return this.deadline.compareTo(MAX_DEADLINE) > 0 ? MAX_DEADLINE : this.deadline;
            }
        }

        public StubConfig() {
            this.deadline = DEFAULT_DEADLINE;
        }

        public void setDeadline(final Duration deadline) {
            this.deadline = deadline;
        }

        public boolean equals(final Object o) {
            if (o == this) {
                return true;
            } else if (!(o instanceof GrpcServiceInfo.StubConfig)) {
                return false;
            } else {
                GrpcServiceInfo.StubConfig other = (GrpcServiceInfo.StubConfig)o;
                if (!other.canEqual(this)) {
                    return false;
                } else {
                    Object this$deadline = this.getDeadline();
                    Object other$deadline = other.getDeadline();
                    if (this$deadline == null) {
                        if (other$deadline != null) {
                            return false;
                        }
                    } else if (!this$deadline.equals(other$deadline)) {
                        return false;
                    }

                    return true;
                }
            }
        }

        protected boolean canEqual(final Object other) {
            return other instanceof GrpcServiceInfo.StubConfig;
        }

        public int hashCode() {
            boolean PRIME = true;
            int result = 1;
            Object $deadline = this.getDeadline();
            result = result * 59 + ($deadline == null ? 43 : $deadline.hashCode());
            return result;
        }

        public String toString() {
            return "GrpcServiceInfo.StubConfig(deadline=" + this.getDeadline() + ")";
        }
    }

    public static class ChannelConfig {
        private boolean usePlaintext = true;

        public ChannelConfig() {
        }

        public boolean isUsePlaintext() {
            return this.usePlaintext;
        }

        public void setUsePlaintext(final boolean usePlaintext) {
            this.usePlaintext = usePlaintext;
        }

        public boolean equals(final Object o) {
            if (o == this) {
                return true;
            } else if (!(o instanceof GrpcServiceInfo.ChannelConfig)) {
                return false;
            } else {
                GrpcServiceInfo.ChannelConfig other = (GrpcServiceInfo.ChannelConfig)o;
                if (!other.canEqual(this)) {
                    return false;
                } else {
                    return this.isUsePlaintext() == other.isUsePlaintext();
                }
            }
        }

        protected boolean canEqual(final Object other) {
            return other instanceof GrpcServiceInfo.ChannelConfig;
        }

        public int hashCode() {
            boolean PRIME = true;
            int result = 1;
            result = result * 59 + (this.isUsePlaintext() ? 79 : 97);
            return result;
        }

        public String toString() {
            return "GrpcServiceInfo.ChannelConfig(usePlaintext=" + this.isUsePlaintext() + ")";
        }
    }
}
